package cn.smbms.controller;

import cn.smbms.beans.Bill;
import cn.smbms.beans.Provider;
import cn.smbms.beans.User;
import cn.smbms.service.Provider.ProviderService;
import cn.smbms.service.bill.BillService;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("bill")
public class BillController {
    private Logger log =  Logger.getLogger(BillController.class);


    @Autowired
    @Qualifier("billService")
    private BillService billService;
    @Autowired
    @Qualifier("providerService")
    private ProviderService providerService;

/*   @RequestMapping("billList.html")
    public ModelAndView billList(@RequestParam(value = "productName") String productName ,@RequestParam(value = "providerId") String providerId , HttpServletRequest request , HttpServletResponse response){
       // String productName = request.getParameter("productName");
       // String providerId = request.getParameter("providerId");
        Bill bill = new Bill();
        System.out.println("传入参数：" + productName +  providerId );
        bill.setProductName(productName);
        bill.setProviderId(Integer.parseInt(providerId));
        List<Bill> billList = billService.getBillList(bill);
        List<Provider> providerList = providerService.getProviderList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("billList", billList);
        modelAndView.addObject("providerList",providerList);
        modelAndView.setViewName("billlist");
        return  modelAndView;
    }*/
   @RequestMapping("billList.html")
    public ModelAndView billList(){
        List<Bill> billList = billService.getBillList();
        List<Provider> providerList = providerService.getProviderList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("billList", billList);
        modelAndView.addObject("providerList",providerList);
        modelAndView.setViewName("billlist");
        return  modelAndView;
    }
    @RequestMapping("addBill.html")
    public String addBill(){
        return "billadd";
    }
    @RequestMapping("doAddBill.html")
    public  String doAddBill( String billCode ,String productName ,String productUnit ,String productCount,String totalPrice,String providerId ){
        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(Integer.parseInt(productCount));
        bill.setTotalPrice(Integer.parseInt(totalPrice));
        bill.setProviderId(Integer.parseInt(providerId));
        int i = billService.addBill(bill);
        if (i > 0 ){
            return "redirect:/bill/billList.html";
        }else {
             return "redirect:/bill/addBill.html";
        }
    }

    //删除用户
    @RequestMapping("deleteBill.html")
    @ResponseBody
    public  String deleteBill(String billid){
        HashMap<Object, Object> map = new HashMap<>();
        int i = billService.deleteBill(billid);
        Integer id = Integer.parseInt(billid);
        if (id < 0) {
            map.put("delResult","notexist");
        }else {
            if (  i>0 ){
                map.put("delResult","true");
            }else{
                map.put("delResult","false");
            }
        }
        return JSONArray.toJSONString(map);
    }

    //查看
    @RequestMapping("billView.html")
    public  ModelAndView billView(String billid){
        Bill bill = billService.billView(billid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bill",bill);
        modelAndView.setViewName("billview");
        return modelAndView;
    }
    //修改页面，先让页面数据显示出来，在执行用户修改操作
    @RequestMapping("modifyBill.html")
    public  ModelAndView  modifyBill(String billid){
        Bill bill = billService.billView(billid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bill",bill);
        modelAndView.setViewName("billmodify");
        return  modelAndView;
    }
    //执行用户修改操作
    @RequestMapping("doUserModify.html")
    public  String doUserModify(String id, String billCode,String productName ,String productUnit,String productCount ,String totalPrice ,String providerId){
        Bill bill = new Bill();
        bill.setId(Integer.parseInt(id));
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(Integer.parseInt(productCount));
        bill.setTotalPrice(Integer.parseInt(totalPrice));
        bill.setProviderId(Integer.parseInt(providerId));
        int i = billService.doUserModify(bill);
        if (i >0 ) {
            return "redirect:/bill/billList.html";
        }else {
            return "redirect:/bill/modifyBill.html";
        }

    }





}
